﻿@model PokerAssistant.Models.Mäng

@{
    ViewBag.Title = "title";
}

<style>
    
    .cards img {
        width: 40px;
    }
    
   .probability {
       font-size: 75px;
   }
   
   h3 {
       margin: -10px;
       margin-left: 0px;
   }
    
   .cards {
       position: absolute;
       left: 550px;
       top: 30px;
   }
   
   .grey {
       color: grey;
   }
   
   .red {
       color: red;
   }

</style>

<script type="text/javascript" >

    $(document).ready(function () {
        Probability();
        //$('#RoyalFlush').insertAfter('#2Pairs');
    });

    function Probability() {
        $(".cards").children().each(function (a, b) {
            GetProbability(b.id);
        });
        //HackDay special
        var sortedDivs = sort($(".cards").children());
        sortedDivs = sort(sortedDivs);
        sortedDivs = sort(sortedDivs);
        sortedDivs = sort(sortedDivs);
        sortedDivs = sort(sortedDivs);
        sortedDivs = sort(sortedDivs);
        sortedDivs = sort(sortedDivs);
        sortedDivs = sort(sortedDivs);
        sortedDivs = sort(sortedDivs);
        $(".cards").html(sortedDivs);
    }

    // find the largest item and push it to the end of the list
    var sort = function (list) {

        var comparisons = 0,
        swaps = 0;

        for (var i = 0, swapping; i < list.length - 1; i++) {
            comparisons++;
            if (parseFloat(list[i].className) < parseFloat(list[i + 1].className)) {
                // swap
                swapping = list[i + 1];

                list[i + 1] = list[i];
                list[i] = swapping;
                swaps++;
            }
        };
        return list;
    };

    function AnnaKaartJuurde() {
        var kaardiCount = $(".YhisedKaardid .MangijaKaardid").length;

        if (kaardiCount >= 5) {
            return true;
        }

        if (kaardiCount == 0) {
            for (var i = 0; i < 3; i++) {
                AnnaKaarti();
            }
            return true;
        }
        AnnaKaarti();

        return true;
    }

    function AnnaKaarti() {
        var mangijaKaardid = "";
        $(".MangijaKaardid").each(function (a, b) {
            var itemID = b.id;
            mangijaKaardid += itemID + ";";
        });

        $.ajax({
            url: '/poker/GetCard',
            type: 'post',
            async: false,
            data: { cards: mangijaKaardid },
            success: function (resp) {
                var kaardiKood = resp.kaart.KaardiKood;
                $(".YhisedKaardid").append("<img alt='IMAGES' src='Content/cards/" + kaardiKood + ".jpg' class='MangijaKaardid' id='" + kaardiKood + "'>");
            }
        });
        Probability();
    }

    function GetProbability(hand) {

        var mangijaKaardid = "";
        $(".Mangija .MangijaKaardid").each(function (a, b) {
            var itemID = b.id;
            mangijaKaardid += itemID + ";";
        });

        var laualOlevadKaardid = "";
        $(".YhisedKaardid .MangijaKaardid").each(function (a, b) {
            var itemID = b.id;
            mangijaKaardid += itemID + ";";
        });

        $.ajax({
            url: '/poker/Get' + hand + 'Probability',
            type: 'post',
            async: false,
            data: { playerCard: mangijaKaardid, cardOnTable: laualOlevadKaardid },
            success: function (resp) {
                var prop = resp.probability;
                $("#" + hand + " .probability").text(prop + " %");
                var color = "";
                if (parseFloat(prop) <= 2) {
                    color = " grey";
                }
                if (parseFloat(prop) >= 10) {
                    color = " red";
                }
                $("#" + hand).attr("class", prop + color);
            }
        });
    }
</script>

<h2>Tere tulemas <b>PokerAssistant</b> leheküljele</h2>

<b>Mängijad: </b> &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp; @Html.ActionLink("Kombinatsioonid", "Index", "Kombinatsioonid")
@foreach (var mängija in Model.Mängijad) {
    var index = Model.Mängijad.IndexOf(mängija) + 1;
    <div class ="Mangija">       
        <br/>Mängija @index
        @foreach (var mängijaKaart in mängija.Kaardid) {
            <img id="@mängijaKaart.KaardiKood" class="MangijaKaardid" src= "@Url.Content(String.Format("Content/cards/{0}.jpg", mängijaKaart.KaardiKood))" alt="IMAGES" />
        }
    </div>
}
<br/>
<input id="AnnaKaar" type="button" onclick="AnnaKaartJuurde();" value="Anna kaart"/>
<br/>
<b>Kaardid laual: </b><br/>
<div class="YhisedKaardid">
    
</div>
@*@foreach (var kaartLaual in Model.KaardidLaual)
{
    <img src= "@Url.Content(String.Format("Content/cards/{0}.jpg", kaartLaual.KaardiKood))" alt="IMAGES" />
}*@

<br/>
<br/>
<div class="cards">
    <div id="RoyalFlush">
        <h3>Royal flush</h3>
        <img src="/Content/cards/Ah.jpg"/>
        <img src="/Content/cards/Kh.jpg"/>
        <img src="/Content/cards/Qh.jpg"/>
        <img src="/Content/cards/Jh.jpg"/>
        <img src="/Content/cards/Th.jpg"/>
        <span class="probability"></span>
    </div>
    <div id="StraightFlush">
        <h3>Straight flush </h3>
        <img src="/Content/cards/Th.jpg"/>
        <img src="/Content/cards/9h.jpg"/>
        <img src="/Content/cards/8h.jpg"/>
        <img src="/Content/cards/7h.jpg"/>
        <img src="/Content/cards/6h.jpg"/>
        <span class="probability"></span>
    </div>
    <div id="4OfAKind">
        <h3>4 of a kind </h3>
        <img src="/Content/cards/Ah.jpg"/>
        <img src="/Content/cards/Ad.jpg"/>
        <img src="/Content/cards/Ac.jpg"/>
        <img src="/Content/cards/As.jpg"/>
        <img src="/Content/cards/3h.jpg"/>
        <span class="probability"></span>
    </div>
    <div id="FullHouse">
        <h3>Full house </h3>
        <img src="/Content/cards/Kd.jpg"/>
        <img src="/Content/cards/Kc.jpg"/>
        <img src="/Content/cards/8h.jpg"/>
        <img src="/Content/cards/8d.jpg"/>
        <img src="/Content/cards/8s.jpg"/>
        <span class="probability"></span>
    </div>
    <div id="Flush">
        <h3>Flush </h3>
        <img src="/Content/cards/Kh.jpg"/>
        <img src="/Content/cards/Qh.jpg"/>
        <img src="/Content/cards/9h.jpg"/>
        <img src="/Content/cards/5h.jpg"/>
        <img src="/Content/cards/8h.jpg"/>
        <span class="probability"></span>
    </div>
    <div id="Straight">
        <h3>Straight </h3>
        <img src="/Content/cards/9h.jpg"/>
        <img src="/Content/cards/8s.jpg"/>
        <img src="/Content/cards/7s.jpg"/>
        <img src="/Content/cards/6d.jpg"/>
        <img src="/Content/cards/5d.jpg"/>
        <span class="probability"></span>
    </div>
    <div id="3OfAKind">
        <h3>3 of a kind </h3>
        <img src="/Content/cards/Jh.jpg"/>
        <img src="/Content/cards/Jd.jpg"/>
        <img src="/Content/cards/Js.jpg"/>
        <img src="/Content/cards/3s.jpg"/>
        <img src="/Content/cards/5d.jpg"/>
        <span class="probability"></span>
    </div>
    <div id="2Pairs">
        <h3>2 pairs </h3>
        <img src="/Content/cards/Jh.jpg"/>
        <img src="/Content/cards/Jd.jpg"/>
        <img src="/Content/cards/5d.jpg"/>
        <img src="/Content/cards/5h.jpg"/>
        <img src="/Content/cards/Qs.jpg"/>
        <span class="probability"></span>
    </div>
    <div id="1Pair">
        <h3>1 pair </h3>
        <img src="/Content/cards/Jh.jpg"/>
        <img src="/Content/cards/Jd.jpg"/>
        <img src="/Content/cards/Qs.jpg"/>
        <img src="/Content/cards/Ad.jpg"/>
        <img src="/Content/cards/5d.jpg"/>
        <span class="probability"></span>
    </div>
    <div id="HighHand">
        <h3>High hand </h3>
        <img src="/Content/cards/Kh.jpg"/>
        <img src="/Content/cards/5s.jpg"/>
        <img src="/Content/cards/3d.jpg"/>
        <img src="/Content/cards/Td.jpg"/>
        <img src="/Content/cards/8h.jpg"/>
        <span class="probability"></span>
    </div>
</div>
